#!/bin/bash

[[ -n "$LKP_SRC" ]] || LKP_SRC=$(dirname $(dirname $(readlink -e -v $0)))

source $LKP_SRC/lib/result.sh
source $LKP_SRC/lib/constant.sh

shopt -s nullglob

script_name=$(basename $0)

# Tips:
# - to search file under /result/xxx/result/ directory, use `-f result/xxx` option,like below:
#   lkp rt -d 3d -f result/0.json piglit /0/
# - to filter non-existence directory, use `-f .` option,like below:
#   lkp rt -d 3d -f . piglit  /0/
usage()
{
	cat <<-EOF
Usage:
	lkp $script_name [-d N[dwm]] [-f filename] [ -p project ] [patterns...]
Example:
	lkp $script_name -d 3d  lkp-ws02  xfstests /0/
EOF

	exit 1
}

[[ $# = 0 ]] && usage

while getopts "f:d:p:q:" opt
do
	case $opt in
		f ) opt_file="$OPTARG"; ;;
		d ) opt_date="$OPTARG"; ;;
		p ) opt_project="$OPTARG"; ;;
		q ) opt_queue="$OPTARG"; ;;
		? ) usage >&2; ;;
	esac
done

shift $((OPTIND-1))

[[ $opt_date =~ [0-9]+d$ ]] && opt_date=${opt_date%d}
[[ $opt_date =~ [0-9]+w$ ]] && opt_date=$((${opt_date%w} * 7))
[[ $opt_date =~ [0-9]+m$ ]] && opt_date=$((${opt_date%m} * 30))

opt_queue="$opt_queue*"

if [[ $opt_date ]]; then
	files=()
	for d in 0 $(seq $opt_date)
	do
		day=$(date +%F -d "$d days ago")
		if [[ $script_name =~ ^_ ]]; then
			files+=($KTEST_PATHS_DIR/$day/$day-$opt_queue $KTEST_PATHS_DIR/$day/.$day-$opt_queue)
		else
			files+=($KTEST_PATHS_DIR/$day/$day-$opt_queue)
		fi
	done
else
	if [[ $script_name =~ ^_ ]]; then
		files=($KTEST_PATHS_DIR/*/????-??-??-$opt_queue $KTEST_PATHS_DIR/*/.????-??-??-$opt_queue)
	else
		files=($KTEST_PATHS_DIR/*/????-??-??-$opt_queue)
	fi
fi

[[ $files ]] || exit 0

case $opt_project in
	all | ALL  ) projects=$(ls $LKP_SRC/repo/); ;;
	""         ) projects="linux"; ;;
	*          ) projects=$opt_project; ;;
esac

patterns="$*"

temp_file_unsort=$(mktemp /tmp/lkp-rt-unsort-XXXXXX)
temp_file_sort=$(mktemp /tmp/lkp-rt-sort-XXXXXX)

trap "rm -f $temp_file_unsort $temp_file_sort" EXIT

[[ $patterns ]] ||
cat "${files[@]}" >> $temp_file_unsort

[[ $patterns ]] && {
	flag_pattern=0
	for pattern in $patterns
	do
		# expand v3.12 etc. to commit SHA1
		for project in $projects
		do
			pattern=$(expand_tag_to_commit $pattern $project)
		done

		if [[ "$flag_pattern" = "0" ]]; then
			cmd="grep -h -E $pattern ${files[@]}"
			flag_pattern=1
		else
			cmd="$cmd | grep -E $pattern"
		fi
	done
	eval "$cmd" >> $temp_file_unsort
}

[[ -s "$temp_file_unsort" ]] || exit 1

sed -i -e 's#/$##' $temp_file_unsort

case $script_name in
rt|ls|ll|result)
	sort $temp_file_unsort > $temp_file_sort
	;;
_rt|_result)
	awk -F'/' '{OFS="/"; NF=NF-1; print}' $temp_file_unsort | sort -u > $temp_file_sort
	;;
__rt|__result)
	awk -F'/' '{OFS="/"; NF=NF-2; print}' $temp_file_unsort | sort -u > $temp_file_sort
	;;
esac

show_one_path()
{
	case $script_name in
	ls)
		echo "$1"
		ls "$1"
		echo
		;;
	ll)
		echo "$1"
		ls -l "$1"
		echo
		;;
	*)
		echo "$1"
		;;
	esac
}

while read path
do
	[[ ! "$opt_file" || -e "$path/$opt_file" ]] &&
	show_one_path "$path/$opt_file"
done < $temp_file_sort

exit 0
